<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>增加房间</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <script type="text/javascript" src="/js/cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body">
    <form class="layui-form" id="update">
        <div class="layui-form-item">
            <label for="roomNumber" class="layui-form-label">
                房间门号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="roomNumber" name="roomNumber" required=""
                       autocomplete="off" class="layui-input" lay-verify="required|number" >
            </div>
        </div>
        <div class="layui-form-item">
            <label for="roomType" class="layui-form-label">
                房间类型
            </label>
            <div class="layui-input-inline">
                <select name="rtid" id="roomType" class="layui-select-group" lay-verify="required" >
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="clockRoom" class="layui-form-label">
                钟点房状态
            </label>
            <div class="layui-input-inline">
                <select name="clockRoom" id="clockRoom" class="layui-select-group">
                    <option name="status" value="0">否</option>
                    <option name="status" value="1">是</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="clockPrice" class="layui-form-label">
                房间价格
            </label>
            <div class="layui-input-inline">
                <input type="number" id="clockPrice" name="clockPrice" required="" lay-verify="required|number"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="curStatus" class="layui-form-label">
                房间状态
            </label>
            <div class="layui-input-inline">
                <select name="curStatus" id="curStatus" class="layui-select-group">
                    <option name="status" value="0">空闲</option>
                    <option name="status" value="1">入住</option>
                    <option name="status" value="2">维修</option>
                    <option name="status" value="3">废弃</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="roomTel" class="layui-form-label">
                客房电话
            </label>
            <div class="layui-input-inline">
                <input type="tel" id="roomTel" name="roomTel" required="" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center">
            <button class="layui-btn" lay-filter="add" lay-submit="">
                增加
            </button>
            <button class="layui-btn" lay-filter="reset" type="reset">
               重置
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        var $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;
        //自定义验证规则
        form.verify({
            //验证固定号码是否有误
            roomTel: [/\d{3,4}[-]\d{7}/, "房间固定号码格式有误"]
        });
        // $.get({
        //     url: '/roomType/list',
        //     dataType: 'json',
        //     success: function (res) {
        //         console.log(res);
        //         var roomType = res.data;
        //         var selectType = $("#roomType");
        //         roomType.forEach(function (ele, index) {
        //             var option = $('<option></option>');
        //             //如果该类型在下拉框中没有，则添加
        //             if ($('#roomType option[value=' + ele['roomType'] + ']').length == 0) {
        //                 option.val(ele['id']);
        //                 option.text(ele['roomType']);
        //                 option.appendTo(selectType);
        //             }
        //             form.render('select');
        //         });
        //
        //     }
        // });
        //获取父页面的房间类型下拉框选项，克隆一个一样的（不克隆时，下拉框出现bug）
        var roomType = parent.$('#roomType option').clone();
        //把下拉框添加到子页面房间类型处
        $('#roomType').append(roomType);
        //动态插入表单元素之后要手动渲染一次表单才能更新数据
        form.render('select');
        //监听提交
        form.on('submit(add)', function (data) {
            //发异步，把数据提交给后台
            $.ajax({
                url:'/roomDetail/addSave',
                type:'POST',
                data: $('#update').serialize(),
                success:function (res) {
                    console.log($('#update').serialize());
                    if(res.status==0){
                        layer.closeAll('loading');
                        layer.load(2);
                        layer.alert("添加成功", {icon: 6}, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            parent.location.reload();
                        });
                    } else{
                        layer.alert("添加失败", {icon: 5})
                    }
                },
            })
            return false;



        });


    });
</script>

</body>

</html>